package mis510.homefinders;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class sendmail {	
	public  String SendEmail(String email, String firstname, String lastname, String Emailaddress, String phonenumber, String age, String profession, String city, String budget, String foodhabits, String dateofjoin, String gender, String agegroup, String pets) {
		String mailsent = "null";
		System.out.println("in sending email");
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		
		System.out.println("before session");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("findhomeinc@gmail.com","$@feh0me");
				}
			});
		System.out.println("before try");
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("findhomeinc@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setSubject("Roommate Request");
			message.setText("Dear ," + firstname + " feels you can be a potential roommate. Below are the details:" + "\nEmail address: " + Emailaddress + "\nPhone NUmber: " +phonenumber+ "\n Age : " +age+ "\n Profession : " +profession+ "\n City : " +city+ "\n Budget : "
 +budget+ "\n food habits" +foodhabits+ " \nDate of Join : " +dateofjoin+	"\nGender Preference :"	+gender+ "\n Preferred age group" +agegroup+ "\n Pets:" +pets+ "\n\n Please feel free to contact me");
 
			Transport.send(message);
            mailsent = "Mail sent successfully";
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
		return mailsent;
	}

}
